package com.xuqm.sdhbwfu.app.viewmodel

import android.R.attr
import android.graphics.Bitmap
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xuqm.base.di.manager.HttpManager
import com.xuqm.base.extensions.showMessage
import com.xuqm.sdhbwfu.app.model.PmTaskContent
import com.xuqm.sdhbwfu.app.model.PmTaskModel
import com.xuqm.sdhbwfu.app.model.SginModel
import com.xuqm.sdhbwfu.app.repository.Service
import com.xuqm.sdhbwfu.core.extensions.io_main
import com.xuqm.sdhbwfu.core.extensions.subscribeBy
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import android.graphics.Bitmap.CompressFormat
import java.io.ByteArrayOutputStream
import android.R.attr.path

import android.graphics.BitmapFactory
import com.xuqm.sdhbwfu.app.data.*


class Maintain1ViewModel : BaseViewModel() {


    private val _status_sgin = MutableLiveData<SginModel>()
    val status_sgin: LiveData<SginModel> = _status_sgin

    fun signUpload(img: Bitmap) {
        val bos = ByteArrayOutputStream()
        img.compress(CompressFormat.JPEG, 100, bos)
        val data: ByteArray = bos.toByteArray()
        val fileBody: RequestBody =
            RequestBody.create(MediaType.parse("application/octet-stream"), data)
        val body = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", "", fileBody)
            .build()

        HttpManager.getApi(Service::class.java)
            .signUpload(body)
            .io_main()
            .subscribeBy(
                {
                    _status_sgin.postValue(it)
                }, {
                    _status_sgin.postValue(null)
                    showMessage("上传签名图片失败::$it")
                }
            ).adds()
    }

    private val _status_err = MutableLiveData<SginModel>()
    val status_err: LiveData<SginModel> = _status_err
    fun errUpload(path: String) {

        val options = BitmapFactory.Options()
        val bitmap = BitmapFactory.decodeFile(path, options)
        val outStream = ByteArrayOutputStream() //准备用来存放压缩图片的位数组
        bitmap.compress(CompressFormat.JPEG, 50, outStream)
        val data: ByteArray = outStream.toByteArray()
        val fileBody: RequestBody =
            RequestBody.create(MediaType.parse("application/octet-stream"), data)
        val body = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", "", fileBody)
            .build()

        HttpManager.getApi(Service::class.java)
            .signUpload(body)
            .io_main()
            .subscribeBy(
                {
                    _status_err.postValue(it)
                }, {
                    _status_err.postValue(null)
                    showMessage("上传签名图片失败::$it")
                }
            ).adds()
    }


    private val _status = MutableLiveData<Boolean>()
    val status: LiveData<Boolean> = _status

    fun update(item: PmTaskContent) {
        HttpManager.getApi(Service::class.java)
            .updatePmTask(item)
            .io_main()
            .subscribeBy(
                {
                    _status.postValue(true)
                }, {
                    _status.postValue(false)
                    showMessage(it)
                }
            ).adds()
    }

    fun submitExecutable(item: PmTaskContent) {
        HttpManager.getApi(Service::class.java)
            .submitExecutable(item)
            .io_main()
            .subscribeBy(
                {
                    _status.postValue(true)
                }, {
                    _status.postValue(false)
                    showMessage(it)
                }
            ).adds()
    }

    private val _statusSign = MutableLiveData<String>()
    val statusSign: LiveData<String> = _statusSign

    fun submitFirst(item: SignData) {
        HttpManager.getApi(Service::class.java)
            .submitFirst(item)
            .io_main()
            .subscribeBy(
                {
                    _statusSign.postValue(it?.endTime)
                }, {
                    _statusSign.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _statusFinish = MutableLiveData<String>()
    val statusFinish: LiveData<String> = _statusFinish

    fun finish(id: Int) {
        HttpManager.getApi(Service::class.java)
            .finish(FinishData(id))
            .io_main()
            .subscribeBy(
                {
                    _statusFinish.postValue(it?.endTime)
                }, {
                    _statusFinish.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _statusCreate = MutableLiveData<String>()
    val statusCreate: LiveData<String> = _statusCreate
    fun createItem(mainId: Int, assetCode: String, assetName: String) {
        HttpManager.getApi(Service::class.java)
            .createItem(CreateItemData(mainId, assetCode, assetName))
            .io_main()
            .subscribeBy(
                {
                    _statusCreate.postValue(it?.endTime)
                }, {
                    _statusCreate.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }
    private val _statusRefresh = MutableLiveData<String>()
    val statusRefresh: LiveData<String> = _statusRefresh
    fun refresh(id: String) {
        HttpManager.getApi(Service::class.java)
            .refresh(id)
            .io_main()
            .subscribeBy(
                {
                    _statusRefresh.postValue(it?.endTime)
                }, {
                    _statusRefresh.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    fun submitSafety(item: SignSafetyData) {
        HttpManager.getApi(Service::class.java)
            .submitSafety(item)
            .io_main()
            .subscribeBy(
                {
                    _statusSign.postValue(it?.endTime)
                }, {
                    _statusSign.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    fun submitTool(item: SignToolData) {
        HttpManager.getApi(Service::class.java)
            .submitTool(item)
            .io_main()
            .subscribeBy(
                {
                    _statusSign.postValue(it?.endTime)
                }, {
                    _statusSign.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    fun submitMaterial(item: SignMaterialData) {
        HttpManager.getApi(Service::class.java)
            .submitMaterial(item)
            .io_main()
            .subscribeBy(
                {
                    _statusSign.postValue(it?.endTime)
                }, {
                    _statusSign.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    fun submitPm(item: SignPmData) {
        HttpManager.getApi(Service::class.java)
            .submitPm(item)
            .io_main()
            .subscribeBy(
                {
                    _statusSign.postValue(it?.endTime)
                }, {
                    _statusSign.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

    private val _stepMain = MutableLiveData<PmTaskModel.StepMain>()
    val stepMain: LiveData<PmTaskModel.StepMain> = _stepMain

    fun stepMainList(id: String) {
        HttpManager.getApi(Service::class.java)
            .stepMainList(id)
            .io_main()
            .subscribeBy(
                {
                    _stepMain.postValue(it)
                }, {
                    _stepMain.postValue(null)
                    showMessage(it)
                }
            ).adds()
    }

}